/**
 * 考生信息管理界面
 */
<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/manageClass/allRoomsInfo' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>考生信息</el-breadcrumb-item>
    </el-breadcrumb>
     <!-- 搜索筛选 -->
    <el-form :inline="true" :model="formInline" class="user-search">
      <el-form-item label="搜索：">
        <el-input size="small" v-model="searchData.value" placeholder="请输入身份证或者姓名"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button size="small" type="primary" icon="el-icon-search" @click="search">搜索</el-button>
      </el-form-item>
      <el-form-item label="">
      </el-form-item>
       <el-form-item>
        <el-button size="small" type="danger" @click="goDownload" class="but_pdf" icon="el-icon-download">导出当前考试 Excel</el-button>
      </el-form-item>
       <el-form-item>
        <el-select size="small" v-model="idLabel" placeholder="请选择考试编号" @change="changeExamId">
        <el-option
          v-for="item in idOptions"
          :key="item.value"
          :label="item.label"
          :value="item.value">
        </el-option>
        </el-select>
      </el-form-item>
    </el-form>
     <!--列表-->
    <el-table size="small" :data="stuInfoList" highlight-current-row v-loading="loading" border element-loading-text="拼命加载中" style="width: 100%;">
      <el-table-column align="center" sortable prop="sCardNo" label="身份证号" width="150">
      </el-table-column>
      <el-table-column align="center" sortable prop="sCertNo" label="考生学号" width="150">
      </el-table-column>
      <el-table-column  align="center" prop="sName" label="考生姓名" width="100">
      </el-table-column>
      <el-table-column align="center" label="性别" width="70">
      <template slot-scope="scope">
         <span>{{scope.row.sSex === 1 ? '男':'女'}}</span>
        </template>
      </el-table-column>
       <el-table-column align="center"  label="考生照片" width="125">
        <template slot-scope="scope">
          <img :src="scope.row.sPic" class="stu_pic">
        </template>
      </el-table-column>
         <el-table-column align="center"  prop="sSchool" label="所属学校" width="150">
      </el-table-column>
         <el-table-column align="center" sortable prop="erid" label="考场id" width="100">
      </el-table-column>
         <el-table-column align="center" sortable prop="sRoomNo" label="座位号" width="100">
      </el-table-column>
       <el-table-column align="center" prop="sSubject" label="考生专业" width="150">
      </el-table-column>
       <el-table-column  align="center" prop="course" label="报考专业" width="150">
      </el-table-column>
      <el-table-column align="center" label="操作" min-width="100">
        <template slot-scope="scope">
          <el-button size="mini" type="primary" @click="handleEdit(scope.$index, scope.row)" icon="el-icon-edit">修改</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <Pagination :child-msg="pageparm" @callFather="callFather"></Pagination>
     <!-- 编辑界面 -->
    <el-dialog :title="title" :visible.sync="stuFormVisible" width="30%" @click="closeDialog">
      <el-form label-width="102px" :model="stuForm"  ref="stuForm">
        <el-form-item label="身份证号" prop="sCardNo">
          <el-input size="small" v-model="stuForm.sCardNo" auto-complete="off" placeholder="身份证号"></el-input>
        </el-form-item>
        <el-form-item label="考生学号" prop="sCertNo">
          <el-input size="small" v-model="stuForm.sCertNo" auto-complete="off" placeholder="考生学号"></el-input>
        </el-form-item>
         <el-form-item label="考生姓名" prop="sName">
          <el-input size="small" v-model="stuForm.sName" auto-complete="off" placeholder="考生姓名"></el-input>
        </el-form-item>
        <el-form-item label="性别" prop="sSex">
          <el-select v-model="stuForm.sSex">
           <el-option
             v-for="item in options"
             :key="item.value"
             :label="item.label"
             :value="item.value">
           </el-option>
           </el-select>
        </el-form-item>
         <el-form-item label="考生照片" prop="sPic">
          <el-upload class="upload-demo" action="http://127.0.0.1/EXAM/onePictureUpload/" :headers = "Myheaders" :on-success="handleSuccessElUpload">
           <el-button slot="trigger" size="small" type="primary">选取照片</el-button>
         </el-upload>
        </el-form-item>
        <el-form-item label="所属学校" prop="sSchool">
          <el-input size="small" v-model="stuForm.sSchool" auto-complete="off" placeholder="所属学校"></el-input>
        </el-form-item>
         <el-form-item label="考场id" prop="erid">
          <el-input size="small" v-model="stuForm.erid" auto-complete="off" placeholder="考场id"></el-input>
        </el-form-item>
         <el-form-item label="座位号" prop="sRoomNo">
          <el-input size="small" v-model="stuForm.sRoomNo" auto-complete="off" placeholder="座位号"></el-input>
        </el-form-item>
         <el-form-item label="考生专业" prop="sSubject">
          <el-input size="small" v-model="stuForm.sSubject" auto-complete="off" placeholder="考生专业"></el-input>
        </el-form-item>
         <el-form-item label="报考专业" prop="course">
          <el-input size="small" v-model="stuForm.course" auto-complete="off" placeholder="报考专业"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click="closeDialog">取消</el-button>
        <el-button size="small" type="primary" :loading="loading" @click="modinfo()">保存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getAllStuinfo, getExamProjectInfo, searchStu, modiStuInfo } from '../../../api/userOpe'
import Pagination from '../../../components/Pagination'
export default {
  data () {
    return {
      loading: true, // 是显示加载
      stuFormVisible: false, // 控制编辑页面显示与隐藏
      title: '修改考生信息',
      // 图片压缩包 提交携带 token
      Myheaders: {
        USERTOKEN: sessionStorage.getItem('token')
      },
      // 储存考试代码编号和名字 后续做成选择框
      examInfo: [],
      // 储存 eStatus = 1 的 考试编号
      activeData: '',
      coureId: '',
      // 存储获取的学生数据
      stuInfoList: [],
      // 要搜索的值
      searchData: {
        value: ''
      },
      // 考试编号选择器数据
      idOptions: [],
      idData: { value: '', label: '' }, // for 循环 放进 idOption
      // 默认 idLabel 值
      idLabel: '',
      // 学生性别选择器
      options: [{
        value: '1',
        label: '男'
      }, {
        value: '2',
        label: '女'
      }],
      // 学生修改信息表单
      stuForm: {
        sCardNo: '',
        sCertNo: '',
        sName: '',
        sSex: '',
        sPic: '',
        sSchool: '',
        erid: '',
        sRoomNo: '',
        sSubject: '',
        course: ''
      },
      // 表单数据发送
      formInline: {
        pageNum: 1,
        pageSize: 10
      },
      // 分页参数
      pageparm: {
        currentPage: 1,
        pageSize: 10,
        total: 0
      }
    }
  },
  // 注册组件
  components: {
    Pagination
  },
  created () {
    this.getData()
    // 延时执行，不然拿不到 数组.length
    setTimeout(() => {
      this.dataToSelect()
    }, 1500)
  },
  methods: {
    // 导出excel 表格
    goDownload () {
      const eno = this.activeData[0].eno
      const url = 'http://127.0.0.1/EXAM/exportExamExcel/' + eno + '/0' + '/?USERTOKEN=' + window.sessionStorage.getItem('token')
      window.location.href = url
    },
    // 获取所有学生信息教室
    getData () {
      getExamProjectInfo().then(res => {
        // console.log(res)
        for (let i = 0; i < res.data.examInfoList.length; i++) {
          this.examInfo[i] = res.data.examInfoList[i]
        }
        // 筛选 estatus为 1 的 考试数据
        this.activeData = res.data.examInfoList.filter((item) => {
          return item.eStatus === 1
        })
        // console.log(this.activeData)
        this.getInfo(this.activeData[0].eno)
      }).catch((e) => {
        this.$message.warning('请求数据异常，请刷新页面重试！')
      })
    },
    // 获取所有考生信息
    getInfo (eno) {
      getAllStuinfo(eno, this.formInline).then(res => {
        if (res.message !== 'OK') {
          this.$message.error('获取考生信息失败，请重试！')
          this.loading = false
        } else {
          this.$message.success('获取考生信息成功！')
          this.stuInfoList = res.data.studentList.list
          // console.log(this.stuInfoList)
          this.pageparm.total = res.data.studentList.total
          this.loading = false
        }
      }).catch((e) => {
        this.loading = false
        this.$message.warning('请求数据异常，请刷新页面重试！')
      })
    },
    // 给考试编号选择器option赋值
    dataToSelect () {
      // console.log(this.examInfo)
      // console.log(this.examInfo.length)
      for (let i = 0; i < this.examInfo.length; i++) {
        this.idData = {} // 重点 需要把 object初始化 {} 一下，不然赋值完以后，数组的值全部变成最后赋的值
        this.idData.value = this.examInfo[i].eno
        this.idData.label = this.examInfo[i].eName
        this.idOptions[i] = this.idData
      }
      this.idLabel = this.activeData[0].eName // 默认 label 赋值
      // console.log(this.idOptions)
    },
    // select 选择器被选中触发事件 传递value
    changeExamId (val) {
      if (val === this.activeData[0].eno) { // 选中的和上次相同则不刷新网页
        return true
      } else {
        this.activeData[0].eno = val
        // 每次切换格式化到第一页，人性化
        this.formInline.pageNum = 1
        this.pageparm.currentPage = 1
        // this.getInfo(this.activeData[0].eno)
        this.getInfo(this.activeData[0].eno)
      }
    },
    // 搜索考生
    search () {
      if (this.searchData.value.trim() !== '') {
        searchStu(this.searchData).then(res => { // 这里特例，交给后端 首尾空格处理
          this.stuInfoList = res.data.students
        })
      } else {
        this.getInfo(this.activeData[0].eno)
      }
    },
    // 批量上传至服务器
    submitUpload () {
      this.$refs.upload.submit()
    },
    // 批量上传成功钩子,接收返回的后端数据
    handleSuccess (res) {
      // console.log(res)
      if (res.message === 'err') this.$message.error('您上传的文件有误！请重新上传，仅支持压缩文件')
      else {
        this.$message.success('文件上传至服务器成功！')
        // 重新获取数据并渲染
        this.getInfo(this.activeData[0].eno)
      }
    },
    // 考生修改照片成功钩子
    handleSuccessElUpload (res) {
      // console.log(res)
      this.stuForm.sPic = res.data.filePath
    },
    // 打开编辑器，并且预处理
    handleEdit (index, row) {
      this.stuFormVisible = true
      this.stuForm.sid = row.sid
      this.stuForm.sCardNo = row.sCardNo
      this.stuForm.sCertNo = row.sCertNo
      this.stuForm.sName = row.sName
      this.stuForm.sSex = row.sSex === 1 ? '男' : '女'
      this.stuForm.sPic = row.sPic
      this.stuForm.sSchool = row.sSchool
      this.stuForm.erid = row.erid
      this.stuForm.sRoomNo = row.sRoomNo
      this.stuForm.sSubject = row.sSubject
      this.stuForm.course = row.course
    },
    // 修改考生信息
    modinfo () {
      // 预处理
      this.stuForm.sSex = this.stuForm.sSex === '男' ? 1 : 2
      modiStuInfo(this.stuForm).then(res => {
        // console.log(res)
        this.getInfo(this.activeData[0].eno)
        this.stuFormVisible = false
      })
    },
    // 分页插件事件
    callFather (parm) {
      this.formInline.pageNum = parm.currentPage
      this.formInline.pageSize = parm.pageSize
      // console.log(this.formInline)
      this.getInfo(this.activeData[0].eno)
    },
    // 关闭编辑、添加弹出框
    closeDialog () {
      this.stuFormVisible = false
    }
  }
}
</script>

<style lang="stylus" scoped>
.user-search
  margin-top: 20px
.userRole
  width: 100%
.el-table, .el-table__expanded-cell
  opacity : 0.8
.stu_pic
  width:100px
  height :113px
.upload
  margin-left: 15px
</style>
